package com.hsd.mapper;

import com.hsd.entity.Book;
import com.hsd.provider.BookMapperProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface BookMapper {

    /**
     * 通过分类ID查询所有该分类下的书本信息
     */
    @Results({
            @Result(id = true, property = "id", column = "b_id"),
            @Result(property = "name", column = "b_name"),
            @Result(property = "author", column = "b_author"),
            @Result(property = "price", column = "b_price"),
            @Result(property = "number", column = "b_number"),
            @Result(property = "time", column = "b_time"),
            @Result(property = "descr", column = "b_descr"),
            @Result(property = "category", column = "b_category",
                    one = @One(select = "com.hsd.mapper.CategoryMapper.selectByID"))
    })
    @Select("select * from t_book where b_category = #{cid}")
    List<Book> selectBookByCID(Integer cid);

    /**
     * 通过分类ID查询所有该分类下的书本信息
     */
    @Results({
            @Result(id = true, property = "id", column = "b_id"),
            @Result(property = "name", column = "b_name"),
            @Result(property = "author", column = "b_author"),
            @Result(property = "price", column = "b_price"),
            @Result(property = "number", column = "b_number"),
            @Result(property = "time", column = "b_time"),
            @Result(property = "descr", column = "b_descr"),
            @Result(property = "category", column = "b_category",
                    one = @One(select = "com.hsd.mapper.CategoryMapper.selectByID"))
    })
    @Select("select * from t_book where b_id = #{bid}")
    Book selectByID(Integer bid);

    @Results(id = "categoryRM", value = {
            @Result(id = true, property = "id", column = "b_id"),
            @Result(property = "name", column = "b_name"),
            @Result(property = "author", column = "b_author"),
            @Result(property = "price", column = "b_price"),
            @Result(property = "number", column = "b_number"),
            @Result(property = "time", column = "b_time"),
            @Result(property = "descr", column = "b_descr")
    })
    @Select("select * from t_book where b_category = #{cid}")
    List<Book> selectByCID(Integer cid);

    /**
     * 动态SQL之组合查询
     *
     * @param book
     * @return
     */
    @ResultMap(value = "categoryRM")
    @SelectProvider(type = BookMapperProvider.class, method = "getSelectDynamicSQL")
    List<Book> selectByDynamicSQL(Book book);

    /**
     * 动态SQL之添加
     *
     * @param book
     * @return
     */
    @InsertProvider(type = BookMapperProvider.class, method = "getInsertDynamicSQL")
    int insertByDynamicSQL(List<Book> book);

    /**
     * 动态SQL之批量删除
     *
     * @param arr
     * @return
     */
    @DeleteProvider(type = BookMapperProvider.class, method = "getDeleteDynamicSQL")
    int deleteByDynamicSQL(int[] arr);

    /**
     * 动态SQL之选择更新
     *
     * @param book
     * @return
     */
    @UpdateProvider(type = BookMapperProvider.class, method = "getUpdateDynamicSQL")
    int updateByDynamicSQL(Book book);
}
